<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;

use app\common\controller\Api;
use app\common\model\EngineerVoice;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\common\model\HandCard;
use app\common\model\ProductOrder;
use app\common\model\Room as RoomModel;
use app\common\service\ZipFileService;
use app\web\controller\Express;
use think\Exception;
use think\facade\Env;
use think\Db;


/**
 * 前台首页控制器
 * @package app\index\controller
 */
class Room extends Auth
{
    public $noNeedLogin = ['roomZip'];

    /**
     *获取房间类型
     * @return array
     */
    public function getRoomType()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            if (isset($getData['top']) && $getData['top'] == '1') {
                $where['pid'] = '0';
            } else {
                $where[] = ['pid', 'neq', '0'];
            }

            $data = Db::name('qt_room_type')
                ->field('id,name,pid,sort')
                ->where(['store_id' => $userInfo['store_id'], 'status' => '1'])
                ->where($where)
                ->order('sort asc')
                ->select();
            foreach ($data as $key => $value) {
                if ($value['pid'] == '0') {
                    $data[$key]['father_name'] = '顶级分类';
                } else {
                    $data[$key]['father_name'] = Db::name('qt_room_type')->where('id', $value['pid'])->value('name');
                }
            }
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 删除房间类型
     * @return array
     */
    public function type_delete()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['id'])) {
                $this->result('缺少参数', '', 204);
            }
            $roomFind  = Db::name('qt_room')->where(['type_id'=>$getData['id']])->find();
            if(!empty($roomFind)){
                $this->result('该分类下面有房间，禁止删除', '', 204);
            }
            $del = Db::name('qt_room_type')->where('id', $getData['id'])->delete();
            if ($del) {
                $this->result('删除成功', '', 200);
            }
            $this->result('删除失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 修改房间类型（获取数据）
     * @return array
     */
    public function type_edit()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $info = Db::name('qt_room_type')->where('id', $getData['id'])->find();
            $this->result('查询成功', $info, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 修改房间类型（获取数据）
     * @return array
     */
    public function clear_room()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $info = Db::name('qt_room_type')->where('id', $getData['id'])->find();
            $this->result('查询成功', $info, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 修改房间类型（保存数据）
     * @return array
     */
    public function type_save()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['id']) || empty($getData['pid']) || empty($getData['sort'])|| empty($getData['name'])){
                $this->result('缺少参数', '', 204);
            }
            $info = Db::name('qt_room_type')->where('id', $getData['id'])->update($getData);
            $this->result('更新成功', $info, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 添加房间类型
     * @return array
     */
    public function type_add()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['name']) || empty($getData['pid'])) {
                $this->result('缺少参数', '', 204);
            }
            if ($getData['pid'] == '0') {
                $this->result('禁止添加顶级分类', '', 204);
            }
            $find = Db::name('qt_room_type')->where('id', $getData['pid'])->find();
            if (empty($find)) {
                $this->result('顶级分类不存在', '', 204);
            }
            $check = Db::name('qt_room_type')->where(['name'=>$getData['name'],'store_id'=>$this->userInfo['store_id']])->find();
            if(!empty($check)){
                $this->result('当前分类已经存在，请勿重复添加', '', 204);
            }
            $getData['created_time'] = time();
            $insert = Db::name('qt_room_type')->insert($getData);
            $this->result('添加成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 获取房间详情接口
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_room_info () {

        if ($this->request->isPost()) {
            $data = Db::name('qt_room')->alias('room')
                ->field('room.*,rs.name as status_name')
                ->leftJoin('qt_room_status rs','room.status_id=rs.id')
                ->where(['room.store_id' => $this->userInfo['store_id'], 'room.is_deleted' => '2'])
                ->order('room.id asc')
                ->select();
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     *获取房间状态
     * @return array
     */
    public function getRoomStatus()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $data = Db::name('qt_room_status')
                ->field('id,name,color')
                ->where(['store_id' => $userInfo['store_id'], 'status' => '1'])
                ->order('sort asc')
                ->select();
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     *开房间
     */
    public function open()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            if (empty($getData['room_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $data = Db::name('qt_room')
                ->field('id,name,status_id')
                ->where(['id' => $getData['room_id'], 'store_id' => $userInfo['store_id']])
                ->find();
            if (empty($data) ||  !in_array($data['status_id'],['1','9']) ) {
                $this->result('房间状态有问题', '', 204);
            }
            try {
                Db::startTrans();
                //修改房间状态
                Db::name('qt_room')->where(['id' => $getData['room_id']])->update(['status_id' => '10']);
                $roomLog['room_id'] = $getData['room_id'];
                $roomLog['user_id'] = $userInfo['id'];
                $roomLog['status'] = '2';
                $roomLog['created_time'] = time();
                Db::name('qt_room_log')->insert($roomLog);
                Db::commit();
            } catch (Exception $exception) {
                Db::rollback();
                $this->result('开房失败', '', 204);
            }
            $this->result('开房成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 房间备注
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remarks()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['room_id'])) {
                $this->result('缺少房间号', '', 204);
            }
            $update = Db::name('qt_room')->where(['id' => $getData['room_id']])->update(['content' => $getData['content']]);
            if ($update !== false) {
                $this->result('备注成功', '', 200);
            }
            $this->result('备注失败，请稍后再试', '', 204);
        }
    }


    /**
     * 房间预约
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function appointment()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!empty($getData['room_id'])){
                $roomRes = Db::name('qt_room')->where('id',$getData['room_id'])->find();
            }else{
                $roomRes['status_id'] = '';
            }

            $getData['created_time'] = time();
            $getData['time'] = strtotime($getData['time']);
            $getData['creater'] = $this->userInfo['id'];
            try {
                Db::startTrans();
                //修改订单状态
                $appointmentId = Db::name('qt_room_appointment')->insertGetId($getData);
                if($roomRes['status_id'] == '1'){
                    Db::name('qt_room')->where('id', $getData['room_id'])->update(['status_id' => '9']);
                }
                Db::commit();
            } catch (Exception $exception) {
                Db::rollback();
                $this->result('预约失败', '', 204);
            }
            //插入预约播报
            EngineerVoice::sendAppointmentVoice($appointmentId,'1');

            $this->result('预约成功', '', 200);
        }
    }

    /**
     * 取消预约
     */
    public function del_appointment()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['id'])) {
                $this->result('缺少预约信息', '', 204);
            }
            $find = Db::name('qt_room_appointment')->where('id', $getData['id'])->find();
            $userStatus = Db::name('qt_users')->where('id', $find['engineer_id'])->value('work_status');
            if (empty($find)) {
                $this->result('未找到房间预约信息', '', 204);
            }
            try {
                Db::startTrans();
                //修改订单状态
                Db::name('qt_room_appointment')->where('id', $getData['id'])->update(['status' => '3']);
                Db::name('qt_room')->where('id', $find['room_id'])->update(['status_id' => '1']);
                if($userStatus == '3'){
                    Db::name('qt_uses')->where('id', $find['engineer_id'])->update(['work_status' => '1']);
                }
                Db::commit();
            } catch (Exception $exception) {
                Db::rollback();
                $this->result('取消失败', '', 204);
            }
            //插入预约播报
            EngineerVoice::sendAppointmentVoice($getData['id'],'2');
            $this->result('取消成功', '', 200);
        }
    }


    /**
     * 首页取消预约
     */
    public function home_appointment()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['id'])) {
                $this->result('缺少预约信息', '', 204);
            }
            $find = Db::name('qt_room_appointment')->where('room_id', $getData['id'])->find();
            $userStatus = Db::name('qt_users')->where('id', $find['engineer_id'])->value('work_status');
            if (empty($find)) {
                $this->result('未找到房间预约信息', '', 204);
            }
            try {
                Db::startTrans();
                //修改订单状态
                Db::name('qt_room_appointment')->where('room_id', $getData['id'])->update(['status' => '3']);
                Db::name('qt_room')->where('id', $getData['id'])->update(['status_id' => '1']);
                if($userStatus == '3'){
                    Db::name('qt_uses')->where('id', $find['engineer_id'])->update(['work_status' => '1']);
                }
                Db::commit();
            } catch (Exception $exception) {
                Db::rollback();
                $this->result('取消失败', '', 204);
            }
            $this->result('取消成功', '', 200);
        }
    }


    /**
     * 确认到达
     */
    public function confirm_appointment()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['id'])) {
                $this->result('缺少预约信息', '', 204);
            }
            $find = Db::name('qt_room_appointment')->where('id', $getData['id'])->find();
            //技师不是空闲状态无法确认到达
            if(!empty($find['engineer_id'])){
                $projectRes = Db::name('qt_project')->where('id',$getData['project_id'])->find();
                $roomStatus = Db::name('qt_users')->where(['id'=>$find['engineer_id']])->value('work_status');
                if(in_array($roomStatus,[1,3]) == false){
                    $this->result('技师不是空闲状态', '', 204);
                }
            }
            //如果预定的房间和到达房间一致
            if(!empty($getData['room_id'])){
                $update['room_id'] =  $getData['room_id'];
                if($getData['is_call'] == '1'){
                    $orderType = '2';
                }else{
                    $orderType = '3';
                }
                //如果存在项目的话给技师添加项目
                if(!empty($getData['project_id']) || !empty($find['project_id'])){
                    $projectRes = Db::name('qt_project')->where('id',$getData['project_id'])->find();
                    $update['project_id'] =  $getData['project_id'];
                    if(isset($getData['engineer_id']) && !empty($getData['engineer_id'])){
                        $engineerWorkStatus = Db::name('qt_users')->where(['id'=>$getData['engineer_id']])->value('work_status');
                        if(in_array($engineerWorkStatus,[1,3]) == false){
                            $this->result('技师不是空闲状态', '', 204);
                        }
                        $engineerId = $getData['engineer_id'];
                    }else{
                        $engineerId = $find['engineer_id'];
                    }
                    try {
                        $orderCheck = Db::name('qt_order')->where(['room_id' => $getData['room_id'], 'pay_status' => '1'])->find();
                        if (empty($orderCheck)) {
                            $order['order_no'] = ordersn();
                            $order['store_id'] = $this->userInfo['store_id'];
                            $order['room_id'] = $getData['room_id'];
                            $order['hand_card_id'] = '';
                            $order['total_num'] = '1';
                            $order['total_price'] = $projectRes['price'];
                            $order['status'] = '1';
                            $order['order_type'] = $orderType;
                            $order['delay'] = 0;
                            $order['time_type'] = '1';
                            $order['created_time'] = time();
                            $orderId = Db::name('qt_order')->insertGetId($order);
                        } else {
                            $orderId = $orderCheck['id'];
                        }
                        //生成订单附表
                        $orderProject = [
                            'name' => $projectRes['name'],
                            'created_order_id' => $this->userInfo['id'],
                            'order_id' => $orderId,
                            'engineer_id' => $engineerId,
                            'project_id' => $getData['project_id'],
                            'room_id' => $getData['room_id'],
                            'hand_card_id' => '',
                            'order_type' => $orderType,
                            'num' => '1',
                            'price' => $projectRes['price'],
                            'store_id' => $projectRes['store_id'],
                            'creater_room' => $this->userInfo['id'],
                            'created_time' => time(),
                            'sale_id' => $this->userInfo['id'],
                        ];
                        Db::name('qt_order_project')->insert($orderProject);
                        $project = Db::name('qt_order_project')->where('order_id', $orderId)->find();
                        $store = Db::name('qt_store')->where('id', $project['store_id'])->find();
                        $model  = new \app\common\model\Order();
                        $result = $model->scheduling($project, '4', $store, $engineerId);
                        if($result['code'] == '204'){
                            $this->result($result['msg'], '', 204);
                        }
                    }catch (Exception $exception) {
                        Db::rollback();
                        return ['code' => 204, 'msg' => '技师排班失败'];
                    }
                }
            }
            $update['status'] =  '2';
            try {
                Db::startTrans();
                //修改订单状态
                Db::name('qt_room_appointment')->where('id', $getData['id'])->update($update);
                Db::commit();
            } catch (Exception $exception) {
                Db::rollback();
                $this->result('取消失败', '', 204);
            }
            $this->result('确认到达', '', 200);
        }
    }


    /**
     * 修改房间预约
     */
    public function edit_appointment()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['id'])) {
                $this->result('缺少预约信息', '', 204);
            }
            $data = Db::name('qt_room_appointment')
                ->field('ra.*,r.name as room_name,p.name as project_name,u.work_number')
                ->alias('ra')
                ->leftJoin('qt_room r','r.id = ra.room_id')
                ->leftJoin('qt_project p','p.id = ra.project_id')
                ->leftJoin('qt_users u','u.id = ra.engineer_id')
                ->where('ra.id', $getData['id'])
                ->find();
            $this->result('查询成功', $data, 200);
        }
    }


    /**
     * 修改房间预约（保存）
     */
    public function save_appointment()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['id'])) {
                $this->result('缺少预约信息', '', 204);
            }
            $data = Db::name('qt_room_appointment')->where('id', $getData['id'])->find();
            if(empty($data)){
                $this->result('缺少预约信息', '', 204);
            }
            if(!empty($getData['engineer_id'])){
                $userStatus = Db::name('qt_users')->where('id', $getData['engineer_id'])->value('work_status');
                if($userStatus == '1' || $userStatus == '3'){
                    Db::name('qt_users')->where('id', $getData['engineer_id'])->update(['work_status'=>'3']);
                }
            }
            if($getData['room_id'] != $data['room_id']){
                //查找老房间状态
                $oldRoomStatus = Db::name('qt_room')->where(['id'=>$data['room_id']])->value('status_id');
                //只有预约房间状态改成空闲
                if($oldRoomStatus == '9'){
                    Db::name('qt_room')->where(['id'=>$data['room_id']])->update(['status_id'=>'1']);
                }
                $newRoomStatus = Db::name('qt_room')->where(['id'=>$getData['room_id']])->value('status_id');
                if($newRoomStatus == '1'){
                    Db::name('qt_room')->where(['id'=>$getData['room_id']])->update(['status_id'=>'9']);
                }
            }
            $getData['time'] = strtotime($getData['time']);
            try {
                Db::startTrans();
                //修改订单状态
                Db::name('qt_room_appointment')->where('id',$getData['id'])->update($getData);
                Db::name('qt_room')->where('id', $getData['room_id'])->update(['status_id' => '9']);
                Db::commit();
            } catch (Exception $exception) {
                Db::rollback();
                $this->result('预约失败', '', 204);
            }
            $this->result('修改成功', '', 200);
        }
    }



    /**
     * 预约列表
     */
    public function list_appointment()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $data = Db::name('qt_room_appointment')
                ->field('ra.id,ra.time,ra.is_call,r.name,u.real_name,p.name as project_name,engineer_id,ra.delay,ra.content,ra.status,ra.creater')
                ->alias('ra')
                ->leftJoin('qt_room r', 'ra.room_id = r.id')
                ->leftJoin('qt_users u', 'ra.user_id = u.id')
                ->leftJoin('qt_project p', 'ra.project_id = p.id')
                ->whereIn('ra.status', ['1'])
                ->where('ra.store_id', $userInfo['store_id'])
                ->select();
            foreach ($data as $key => $value) {
                $data[$key]['engineer'] = Db::name('qt_users')->where('id', $value['engineer_id'])->value('work_number');
                $data[$key]['creater_name'] = Db::name('qt_users')->where('id', $value['creater'])->value('real_name');
                unset($data[$key]['engineer_id']);
            }
            $this->result('查询成功', $data, 200);
        }
    }


    /**
     * 设置房间颜色
     */
    public function color()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['room_id'])) {
                $this->result('缺少参数', '', 204);
            }
            if (!isset($getData['color']) || empty($getData['color'])) {
                $getData['color'] = '';
            }
            try {
                Db::startTrans();
                //修改订单状态
                Db::name('qt_room')->where('id', $getData['room_id'])->update(['color' => $getData['color']]);
                Db::commit();
            } catch (Exception $exception) {
                Db::rollback();
                $this->result('设置失败', '', 204);
            }
            $this->result('设置成功', '', 200);
        }
    }

    /**
     * 更改房间状态
     */
    public function clean()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['room_id']) || !isset($getData['room_id'])) {
                $this->result('缺少参数', '', 204);
            }
            if (empty($getData['status']) || !isset($getData['status'])) {
                $this->result('缺少房间状态', '', 204);
            }
            $roomData = Db::name('qt_room')->where(['id' => $getData['room_id']])->find();

            if ($getData['status'] == '1') {
                if (isset($getData['user_id']) && !empty($getData['user_id'])) {
                    if (isset($getData['bed_num']) && !empty($getData['bed_num']) && $roomData['clean_rule'] == '2') {
                        $insert['single_commission'] = $roomData['clean_price'];
                        $insert['total_commission'] = $roomData['clean_price'] * $getData['bed_num'];
                    } else {
                        $insert['total_commission'] = $roomData['clean_price'];
                    }
                    $insert['open_time'] = Db::name('qt_room_log')
                        ->where(['room_id' => $getData['room_id']])
                        ->order('id desc')
                        ->value('created_time');
                    $insert['store_id'] = $this->userInfo['store_id'];
                    $insert['room_id'] = $getData['room_id'];
                    $insert['user_id'] = $this->userInfo['id'];
                    $insert['clear_user_id'] = $getData['user_id'];
                    $insert['clear_time'] = time();
                    $insert['create_time'] = time();
                    $insert['clear_bed'] = !empty($getData['bed_num']) ? $getData['bed_num'] : '';
                    Db::name('qt_room_clear')->insert($insert);
                }
                //查看房间是否有等位
                $waitCount = Db::name('qt_wait')->where(['room_id' => $getData['room_id'], 'status' => '1'])->count();
                if ($waitCount > 0) {
                    $this->result('房间内有等待，无法取消房间！', '', 204);
                }
                if ( in_array($roomData['status_id'], [2,3]) ) {
                    $this->result('房间内有消费，无法取消房间！', '', 204);
                }
                //查找房间是否有未结账的账单
                $project = Db::name('qt_order_project')
                    ->where(['room_id' => $getData['room_id'], 'pay_status' => '0', 'is_change' => '1'])
                    ->where('is_delete',2)
                    ->where('status', '<', '4')
                    ->whereNotNull('engineer_id')
                    ->find();
                if (!empty($project)) {
                    $this->result('当前房间有未结账项目，请先处理项目结账！', '', 204);
                }
                //查找房间是否有未结账的账单
                $goods = Db::name('qt_product_order_item')
                    ->where(['room_id' => $getData['room_id'], 'pay_status' => '0', 'status' => '1'])
                    ->find();
                if (!empty($goods)) {
                    $this->result('当前房间有未结账商品，请先处理商品结账！', '', 204);
                }
                if ($roomData['marge'] != '') {
                    Db::name('qt_room')->where(['id' => $getData['room_id']])->update(['marge' => '', 'down_time' => null]);
                }
                Db::name('qt_room')->where(['id' => $getData['room_id']])->update(['down_time' => null]);
            }
            if ( in_array( $getData['status'], [5,6,7,8] ) ) {
                //查找房间是否有未结账的账单
                $project = Db::name('qt_order_project')
                    ->where(['room_id' => $getData['room_id'], 'pay_status' => '0'])
                    ->where('status','<','4')
                    ->where('is_delete',2)
                    ->whereNotNull('engineer_id')
                    ->find();
                if (!empty($project)) {
                    $this->result('当前房间有未结账项目，请先处理项目结账！', '', 204);
                }
                //查找房间是否有未结账的账单
                $goods = Db::name('qt_product_order')
                    ->where(['room_id' => $getData['room_id'], 'pay_status' => '0', 'order_status' => '1'])
                    ->find();
                if (!empty($goods)) {
                    $this->result('当前房间有未结账商品，请先处理商品结账！', '', 204);
                }
            }
            if ($getData['status'] == '7') {
                $update['down_time'] = null;
            }
            $update['status_id'] = $getData['status'];
            try {
                Db::startTrans();
                //修改订单状态
                Db::name('qt_room')->where('id', $getData['room_id'])->update($update);
                Db::commit();
            } catch (Exception $exception) {
                Db::rollback();
                $this->result('设置失败', '', 204);
            }
            $this->result('设置成功', '', 200);
        }
    }

    /**
     * 获取商品分类
     */
    public function goodsCate()
    {
        if ($this->request->isPost()) {
                $data = Db::name('qt_goods_category')
                ->field('id,name,sort,description')
                ->where(['store_id' => $this->userInfo['store_id'], 'status' => '1'])
                ->order('sort  asc')
                ->select();
            $this->result('请求成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }



    /**
     * 添加商品分类
     */
    public function goodsCateAdd()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['sort']) || empty($getData['name']) ){
                $this->result('缺少参数', '', 204);
            }
            $getData['create_time'] = time();
            $getData['store_id'] = $this->userInfo['store_id'];
            $find = Db::name('qt_goods_category')->where(['store_id'=>$this->userInfo['store_id'],'name'=>$getData['name']])->find();
            if(!empty($find)){
                $this->result('商品分类已经存在', '', 204);
            }
            Db::name('qt_goods_category')->insert($getData);
            $this->result('添加成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 修改商品分类
     */
    public function goodsCateEdit()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['sort']) || empty($getData['name'])  || empty($getData['id']) ){
                $this->result('缺少参数', '', 204);
            }
            Db::name('qt_goods_category')->where(['id'=>$getData['id']])->update($getData);
            $this->result('修改成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除商品分类
     */
    public function goodsCateDel()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $goods = Db::name('qt_goods')->where('goods_category_id',$getData['id'])->find();
            if(!empty($goods)){
                $this->result('商品分类下还存在商品,无法删除', '', 204);
            }
            Db::name('qt_goods_category')->where(['id'=>$getData['id']])->update(['status'=>'2']);
            $this->result('删除成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 获取商品
     */
    public function goods()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['category_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $data = Db::name('qt_goods')
                ->where(['store_id'=>$this->userInfo['store_id']])
                ->field('id,name,price')
                ->select();
            $this->result('请求成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 添加商品到购物车
     */
    public function add_cart()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $validate = new \app\web\validate\Room();
            if (!$validate->scene('cart')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            if($this->storeInfo['type'] != '1' ){
                if(!isset($getData['hand_card_name']) || empty($getData['hand_card_name'])){
                    $this->result('请填写手牌号', '', 204);
                }
            }
            $orderModel = new ProductOrder();
            $order_data = $orderModel->createOrder($getData, $this->userInfo);
            if ($order_data['status'] == true) {
                //插入消息
                $goodsData = json_decode($getData['goods'],true);
                goodsOrderMessage($this->userInfo['store_id'],$goodsData,$getData['room_id']);

                $getData['order_id'] = $order_data['order_id'];
                //下单打印小票
                //FeiEPrintService::printPc($getData,$this->userInfo);
                $this->result('添加成功', '', 200);
            }else{
                $this->result($order_data['msg'], '', 204);
            }
        } else {
            $this->result('添加失败', '', 204);

        }
    }

    /**
     * 购物车列表
     */
    public function list_cart()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['room_id'] || !isset($getData['room_id']))) {
                $this->result('缺少房间参数', '', 204);
            }
            if (empty($getData['limit'] || !isset($getData['limit']))) {
                $this->result('缺少房间参数', '', 204);
            }
            $data = Db::name('qt_product_cart')
                ->field('pc.id,pc.product_name,pc.product_price,pc.num,pc.discount,r.name as room_name,hc.name as hand_card_name,pc.created_time')
                ->alias('pc')
                ->leftJoin('qt_hand_card hc', 'hc.id = pc.hand_card_id')
                ->leftJoin('qt_room r', 'r.id = pc.room_id')
                ->where(['pc.room_id' => $getData['room_id']])
                ->order('pc.id desc')
                ->paginate($getData['limit']);
            $this->result('添加成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 删除购物车
     */
    public function del_cart()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['id'] || !isset($getData['id']))) {
                $this->result('缺少购物车信息', '', 204);
            }
            $del = Db::name('qt_product_cart')
                ->where(['id' => $getData['id']])
                ->delete();
            if ($del) {
                $this->result('删除成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 添加房间
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $validate = new \app\web\validate\Room();
            if (!$validate->scene('add_room')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $getData['created_time'] = time();
            $getData['store_id'] = $this->userInfo['store_id'];
            $check = RoomModel::checkRoomExist($this->userInfo['store_id'],$getData['name']);
            if(!empty($check)){
                $this->result('房号已存在', '', 204);
            }
            $roomId = Db::name('qt_room')->insertGetId($getData);
            if ($getData['bed_num'] != '0') {
                $roomBedData = [];
                for ($i = 1; $i <= $getData['bed_num']; $i++) {
                    $roomBedData[$i]['store_id'] = $this->userInfo['store_id'];
                    $roomBedData[$i]['room_id'] = $roomId;
                    $roomBedData[$i]['name'] = '0' . $i;
                    $roomBedData[$i]['status'] = '1';
                    $roomBedData[$i]['created_time'] = time();
                }
            }
            $bed = Db::name('qt_room_bed')->insertAll($roomBedData);
            if ($bed) {
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 编辑房间(保存数据)
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $check = RoomModel::checkRoomExist($this->userInfo['store_id'],$getData['name'],$getData['id']);
            if(!empty($check)){
                if($getData['id'] != $check['id']){
                    $this->result('房号已存在', '', 204);
                }
            }
            $insert = Db::name('qt_room')->where('id', $getData['id'])->update($getData);
            if ($insert !== false) {
                $this->result('修改成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 编辑房间（获取数据）
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $data = Db::name('qt_room')->where('id', $getData['id'])->find();
            $data['type_name'] = Db::name('qt_room_type')->where('id', $data['type_id'])->value('name');
            $data['status'] = Db::name('qt_room_status')->where('id', $data['status_id'])->value('name');
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除房间
     */
    public function delete()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['id'])) {
                $this->result('缺少参数', '', 204);
            }
            $roomStatus = Db::name('qt_room')->where('id', $getData['id'])->value('status_id');
            if($roomStatus != '1'){
                $this->result('房间不是空闲状态，不允许删除', '', 204);
            }
            $del = Db::name('qt_room')->where('id', $getData['id'])->delete();
            if ($del) {
                $this->result('删除成功', '', 200);
            }
            $this->result('删除失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 获取房间列表
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['limit']) || empty($getData['limit'])) {
                $getData['limit'] = '10';
            }
            $sql = Db::name('qt_room')
                ->alias('r')
                ->leftJoin('qt_room_type rt', 'rt.id = r.type_id')
                ->field('r.*,rt.name as type_name')
                ->where(['r.store_id' => $this->userInfo['store_id'],'is_deleted'=>'2']);
            if (isset($getData['name']) && !empty($getData['name'])) {
                $sql->whereLike('r.name', '%' . $getData['name'] . '%');
            }
            $data = $sql->paginate($getData['limit'])->each(function ($item) {
                $roomMac = Db::name('qt_room_mac')->where('room_id', $item['id'])->value('mac');
                $item['mac'] = $roomMac;
                return $item;
            });
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 转房
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function transfer()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['old_room_id']) || empty($getData['new_room_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $oldProjectData = Db::name('qt_order')
                ->where(['room_id' => $getData['old_room_id'], 'pay_status' => '1'])
                ->order('id desc')
                ->find();
            $oldRoom = Db::name('qt_room')->where('id', $getData['old_room_id'])->find();
            $newProjectData = Db::name('qt_order')
                ->where(['room_id' => $getData['new_room_id'], 'pay_status' => '1'])
                ->order('id desc')
                ->find();
            if ($getData['all_transfer'] == '1') {
                //更新新房间信息
                Db::startTrans();
                try {
                    /************  处理项目转房 ***************/
                    //新房间有总项目订单
                    if (!empty($newProjectData)) {
                        //老房间内的订单
                        $project_order_data = Db::name('qt_order_project')
                            ->where(['is_delete' => '2', 'is_change' => '1'])
                            ->whereIn('order_id', $oldProjectData['id'])
                            ->select();

                        //更新项目房间
                        foreach ($project_order_data as $key => $value) {
                            Db::name('qt_order_project')->where(['id' => $value['id']])
                                ->update([
                                    'room_id' => $getData['new_room_id'],
                                    'old_room_id' => $value['room_id']??0,
                                    'order_id' => $newProjectData['id']
                                ]);
                            //获取技师
                            if (!empty($engineerId = Db::name('qt_order_project')->where(['id' => $value['id']])->value('engineer_id'))) {
                                Db::name('qt_order_queue')->where(['user_id' => $engineerId])->update(['room_id' => $getData['new_room_id']]);
                            }
                        }
                    } else {
                        //新房间没有项目总订单
                        Db::name('qt_order')->where('id', $oldProjectData['id'])->update(['room_id' => $getData['new_room_id']]);

                        //老房间内的订单
                        $project_order_data = Db::name('qt_order_project')
                            ->where(['is_delete' => '2', 'is_change' => '1'])
                            ->whereIn('order_id', $oldProjectData['id'])
                            ->select();

                        //更新项目房间
                        foreach ($project_order_data as $key => $value) {
                            Db::name('qt_order_project')->where(['id' => $value['id']])
                                ->update([
                                    'room_id' => $getData['new_room_id'],
                                    'old_room_id' => $value['room_id']??0,
                                ]);
                            //获取技师
                            if (!empty($engineerId = Db::name('qt_order_project')->where(['id' => $value['id']])->value('engineer_id'))) {
                                Db::name('qt_order_queue')
                                    ->where(['user_id' => $engineerId])
                                    ->update(['room_id' => $getData['new_room_id']]);
                            }
                        }
                    }

                    /************  处理商品转房 ***************/
                    $goodsOrderOldData = Db::name('qt_product_order')->where(['room_id' => $getData['old_room_id'], 'pay_status' => '0'])->find();
                    //判断新房间是否有商品
                    $goodsOrderNewData = Db::name('qt_product_order')->where(['room_id' => $getData['new_room_id'], 'pay_status' => '0'])->find();
                    if (!empty($goodsOrderNewData)) {
                        //新房间有商品总订单，删除老房间商品总订单
                        Db::name('qt_product_order')->where('id', $goodsOrderOldData['id'])->delete();
                        //所有的自订单
                        $goods_item_data = Db::name('qt_product_order_item')->where('order_id', $goodsOrderOldData['id'])->select();
                        //更新老房间项目到新房间总订单下
                        foreach ($goods_item_data as $key => $value) {
                            Db::name('qt_product_order_item')
                                ->where(['id' => $value['id']])
                                ->update([
                                    'room_id' => $getData['new_room_id'],
                                    'old_room_id' => $value['room_id']??0,
                                    'order_id' => $goodsOrderNewData['id']
                                ]);
                        }
                        //更新老房间项目到新房间总订单下
                        Db::name('qt_product_order_item')->where(['id' => $goodsOrderOldData['id']])->update(['room_id' => $getData['new_room_id'], 'order_id' => $goodsOrderNewData['id']]);
                    } else {
                        //新房间没有商品总订单
                        //更新老商品总订单为新房间
                        Db::name('qt_product_order')->where('id', $goodsOrderOldData['id'])->update(['room_id' => $getData['new_room_id']]);
                        //所有的自订单
                        $goods_item_data = Db::name('qt_product_order_item')->where('order_id', $goodsOrderOldData['id'])->select();
                        //更新老房间项目到新房间总订单下
                        foreach ($goods_item_data as $key => $value) {
                            Db::name('qt_product_order_item')
                                ->where(['id' => $value['id']])
                                ->update([
                                    'room_id' => $getData['new_room_id'],
                                    'old_room_id' => $value['room_id']??0,
                                ]);
                        }
                    }
                    //老房间更改为空闲
                    Db::name('qt_room')->where('id', $getData['old_room_id'])->update(['status_id' => '5']);
                    //新房间更新为老房间状态
                    Db::name('qt_room')->where('id', $getData['new_room_id'])->update(['status_id' => $oldRoom['status_id']]);
                    Db::commit();
                } catch (Exception $exception) {
                    Db::rollback();
                    $this->result('转房失败', '', 204);
                }
            }
            $this->result('转房成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 合并房
     */
    public function combined()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['old_room_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $oldRoomIds = explode(',', $getData['old_room_id']);
            $newRoomIds = explode(',', $getData['new_room_id']);
            //合并房间数据
            $marge = array_merge($oldRoomIds, $newRoomIds);
            $marge = array_unique($marge);
            if (empty($getData['new_room_id'])) {
                //处理多次提交后数值不一致问题
                $oldMarge = Db::name('qt_room')->where('id', $oldRoomIds[0])->value('marge');
                $oldMarge = explode(',', $oldMarge);
                foreach ($oldMarge as $key => $value) {
                    Db::name('qt_room')->where('id', $value)->update(['marge' => '']);
                }
                $this->result('合并房成功', '', 200);
            }
            //处理多次提交后数值不一致问题
            $oldMarge = Db::name('qt_room')->where('id', $oldRoomIds[0])->value('marge');
            if($oldMarge != ''){
                $oldMarge = explode(',', $oldMarge);
                $array = array_diff($oldMarge, $marge);
                if (!empty($array)) {
                    foreach ($array as $key => $value) {
                        $select = Db::name('qt_room')
                            ->where("FIND_IN_SET(" . $value . ",marge)")
                            ->select();
                        if (!empty($select)) {
                            foreach ($select as $k => $v) {
                                Db::name('qt_room')->where('id', $v['id'])->update(['marge' => '']);
                            }
                        }
                    }
                }
            }
            foreach ($marge as $key => $value) {
                $update = ['marge' => implode(',', $marge)];
                Db::name('qt_room')->where('id', $value)->update($update);
            }
            $this->result('合并房成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 取消合并房
     */
    public function removeCombined()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['room_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $margeIds = Db::name('qt_room')->where('id',$getData['room_id'])->value('marge');
            if(!empty($margeIds)){
                //更新所有的房间状态
                $roomIds = explode(',',$margeIds);
                foreach ($roomIds as $key=>$value){
                    Db::name('qt_room')->where('id',$value)->update(['marge'=>'']);
                }
            }
            $this->result('取消成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }




    /**
     * 获取房间列表
     */
    public function engineer_room()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $where = [];
            //房间类型
            if (!empty($getData['type_id'])) {
                $where['rt.id'] = $getData['type_id'];
            }
            //房间名称
            if (!empty($getData['room_name'])) {
                $where['r.name'] = $getData['room_name'];
            }
            //房间状态
            if (!empty($getData['status_id'])) {
                $where['rs.id'] = $getData['status_id'];
            }
            if (empty($where)) {
                $where = 1;
            }
            $total_member = 0;
            $total_price = 0;
            //原始数据
            $originalData = Db::name('qt_room')
                ->alias('r')
                ->leftJoin('qt_room_type rt', 'rt.id = r.type_id')
                ->leftJoin('qt_room_status rs', 'rs.id = r.status_id')
                ->field('r.id,r.name,rt.name as type_name,rs.name as status_name,r.type_id,rs.color,r.status_id,r.color as room_color,r.bed_num,r.marge,r.content,r.toilet,r.down_time')
                ->where(['r.store_id' => $this->userInfo['store_id'],'r.is_deleted'=>'2'])
                ->where($where)
                ->select();
            foreach ($originalData as $key => $value) {
                if ( $value['status_id'] == '2' || $value['status_id'] == '3' || $value['status_id'] == '4' || $value['status_id'] == '10') {
                    $engineer = Db::name('qt_order_queue')
                        ->alias('oq')
                        ->leftJoin('qt_users u', 'oq.user_id = u.id')
                        ->where(['oq.room_id' => $value['id']])
                        ->whereIn('oq.status', ['2', '1','3'])
                        ->column('u.work_number');
                    $engineer = array_unique($engineer);
                    //单个房间项目
                    $project = Db::name('qt_order')
                        ->field('o.id,r.name as room_name,h.name as hand_card_name')
                        ->alias('o')
                        ->leftJoin('qt_room r','r.id = o.room_id')
                        ->leftJoin('qt_hand_card h','h.id = o.hand_card_id')
                        ->where(['room_id'=>$value['id'],'pay_status'=>'1'])
                        ->order('o.id desc')
                        ->find();
                    if(!empty($project)){
                        $projectPrice = Db::name('qt_order_project')
                            ->alias('op')
                            ->leftJoin('qt_order o', 'o.id = op.order_id')
                            ->leftJoin('qt_users u', 'op.engineer_id = u.id')
                            ->where(['o.room_id' => $value['id'],'op.pay_status'=>'0','op.order_id'=>$project['id'],'op.is_delete'=>'2','op.is_change'=> '1',])
                            ->whereIn('op.status', ['2', '1','3'])
                            ->whereNotNull('engineer_id')
                            ->where('engineer_id','neq','0')
                            ->sum('op.price');
                    }else{
                        $projectPrice = 0;
                    }
                    $handCardName = Db::name('qt_order_project')
                        ->alias('op')
                        ->leftJoin('qt_hand_card h','h.id = op.hand_card_id')
                        ->where(['op.order_id'=>$project['id'],'op.pay_status'=>'0','op.room_id' => $value['id']])
                        ->whereIn('op.status', ['2', '1','3'])
                        ->where(['op.is_change'=> '1','op.is_delete'=>'2'])
                        ->group('h.name')
                        ->column('h.name');
                    if(!empty($handCardName)){
                        if($handCardName[0] == null){
                            $handCardName = [];
                        }
                    }
                    //未结账项目数量
                    $projectPay = Db::name('qt_order_project')
                        ->alias('op')
                        ->leftJoin('qt_order o', 'o.id = op.order_id')
                        ->leftJoin('qt_users u', 'op.engineer_id = u.id')
                        ->where(['o.room_id' => $value['id'],'op.pay_status'=>'0','op.is_change'=>'1','op.is_delete'=>'2'])
                        ->whereIn('op.status', ['2', '1','3'])
                        ->whereNotNull('op.engineer_id')
                        ->count();
                    $goodsOrder = Db::name('qt_product_order')
                        ->where(['room_id' => $value['id'],'pay_status'=>'0'])
                        ->order('id desc')
                        ->find();
                    $goodsPrice = Db::name('qt_product_order_item')
                        ->where(['status'=>'1','pay_status'=>'0','room_id'=>$value['id']])
                        ->sum('goods_total_price');
                    $goodsHandCardName = Db::name('qt_product_order_item')
                        ->alias('item')
                        ->leftJoin('qt_hand_card c','c.id = item.hand_card_id')
                        ->where(['item.status'=>'1','item.pay_status'=>'0'])
                        ->where('item.order_id',$goodsOrder['id'])
                        ->group('c.name')
                        ->column('c.name');
                    //未结算商品数量
                    $goodsPay = Db::name('qt_product_order_item')
                        ->where('room_id',$value['id'])
                        ->where(['pay_status'=>'0','status'=>'1'])
                        ->count();
                    $count = $projectPay + $goodsPay;
                    $price = $projectPrice + $goodsPrice;
                    $total_member += count($engineer);
                    $total_price += $price;
                    $originalData[$key]['price'] = $price;
                    $originalData[$key]['count'] = $count;
                    $originalData[$key]['member'] = count($engineer);
                    $handCardData = array_merge($handCardName,$goodsHandCardName);
                    $handCardData = array_unique($handCardData);
                    $originalData[$key]['hand_card_name'] = $handCardData;
                    foreach ($engineer as $k => $v) {
                        if ($v == null) {
                            unset($engineer[$k]);
                        }
                    }
                    //查找各个技师的起钟时间
                    $engineerIds = Db::name('qt_order_queue')
                        ->where('room_id',$value['id'])
                        ->whereNotNull('start_time')
                        ->whereNull('real_time')
                        ->order('order_project_id desc')
                        ->column('end_time');
                    //项目的时间
                    $projectTime = Db::name('qt_order_project')
                        ->alias('op')
                        ->leftJoin('qt_project p', 'p.id = op.project_id')
                        ->where(['op.room_id' => $value['id'],'op.is_change'=>'1'])
                        ->whereIn('op.status', ['2', '3', '1'])
                        ->order('op.id desc')
                        ->column('p.time');
                    $orderCreateTime = Db::name('qt_order_project')
                        ->where(['room_id' => $value['id'],'is_change'=>'1','is_delete'=>'2','pay_status'=>'0'])
                        ->whereIn('status', ['2', '3', '1'])
                        ->order('id desc')
                        ->column('created_time');
                    if(empty($engineerIds)){
                        $originalData[$key]['end_time'] = null;
                    }else{
                        $originalData[$key]['end_time'] = $engineerIds;
                    }
                    //是否有预约
                    $appointment = Db::name('qt_room_appointment')->where(['room_id'=>$value['id'],'status'=>'1'])->find();
                    if(empty($appointment)){
                        $originalData[$key]['appointment'] = 0;//没有预约
                    }else{
                        $originalData[$key]['appointment'] = 1;//有预约
                    }
                    $originalData[$key]['time'] = implode(',', $projectTime);
                    $originalData[$key]['engineer'] = implode(',', $engineer);
                    $originalData[$key]['order_create_time'] = $orderCreateTime;
                    $originalData[$key]['price'] = $price;
                    $wait =  Db::name('qt_wait')->where(['room_id'=>$value['id'],'status'=>'1','delay_time'=>'0'])->count();
                    $originalData[$key]['wait'] = $wait;
                    if(!empty($value['marge'])){
                        $roomIds = explode(',',$value['marge']);
                        $roomName = Db::name('qt_room')->whereIn('id',$roomIds)->column('name');
                        $originalData[$key]['marge_name'] = implode('-',$roomName);
                    }
                    //技师是否有预约
                    $appointment = Db::name('qt_room_appointment')->where(['room_id'=>$value['id'],'status'=>'1'])->find();
                    if(empty($appointment)){
                        $originalData[$key]['appointment'] = 0;//没有预约
                    }else{
                        $originalData[$key]['appointment'] = 1;//有预约
                    }
                } else {
                    $originalData[$key]['member'] = 0;
                    $originalData[$key]['engineer'] = '';
                    $originalData[$key]['price'] = 0;
                    $originalData[$key]['count'] = 0;
                    $originalData[$key]['wait'] = 0;
                    $originalData[$key]['start_time'] = null;
                    $originalData[$key]['appointment'] = 0;//没有预约
                    if(!empty($value['marge'])){
                        $roomIds = explode(',',$value['marge']);
                        $roomName = Db::name('qt_room')->whereIn('id',$roomIds)->column('name');
                        $originalData[$key]['marge_name'] = implode('-',$roomName);
                    }
                }
                $originalData[$key]['room_book'] = Db::name('qt_room_book')->where(['room_id'=>$value['id'],'status'=>'1'])->count();
            }
            //分组展示
            static $tree = [];
            $data = Db::name('qt_room')
                ->alias('r')
                ->leftJoin('qt_room_type rt', 'rt.id = r.type_id')
                ->leftJoin('qt_room_status rs', 'rs.id = r.status_id')
                ->field('rt.name as type_name,rt.id as type_id')
                ->where(['r.store_id' => $this->userInfo['store_id']])
                ->where($where)
                ->group('r.type_id')
                ->order('rt.id asc')
                ->select();
            foreach ($data as $key=>$value){
                $tree[] = $value;
                $child = Db::name('qt_room_type')
                    ->where(['store_id'=>$this->userInfo['store_id'],'pid'=>$value['type_id']])
                    ->field('id as type_id,name as type_name')
                    ->select();
                foreach ($child as $k=>$val){
                    $tree[] = $val;
                }

            }
            $groupData = $tree;
            foreach ($groupData as $key => $value) {
                $data = Db::name('qt_room')
                    ->alias('r')
                    ->leftJoin('qt_room_type rt', 'rt.id = r.type_id')
                    ->leftJoin('qt_room_status rs', 'rs.id = r.status_id')
                    ->field('r.id,r.name,rt.name as type_name,rs.name as status_name,r.type_id,rs.color,r.status_id,r.color as room_color,r.bed_num,r.marge,r.content,r.toilet,r.down_time')
                    ->where(['r.store_id' => $this->userInfo['store_id'], 'r.type_id' => $value['type_id']])
                    ->where($where)
                    ->select();
                if(empty($data)){
                    unset($groupData[$key]);
                }else{
                    foreach ($data as $k => $v) {
                        if ($v['status_id'] == '2' || $v['status_id'] == '3' || $v['status_id'] == '4' || $v['status_id'] == '10') {
                            $engineer = array_unique($engineer);
                            $projectPrice = Db::name('qt_order_project')
                                ->alias('op')
                                ->leftJoin('qt_order o', 'o.id = op.order_id')
                                ->leftJoin('qt_users u', 'op.engineer_id = u.id')
                                ->where(['o.room_id' => $v['id'],'op.pay_status'=>'0'])
                                ->whereIn('op.status', ['2', '1','3'])
                                ->whereNotNull('op.engineer_id')
                                ->where('op.engineer_id','neq','0')
                                ->where(['op.is_change'=> '1','op.is_delete'=>'2'])
                                ->sum('op.price');
                            $goodsOrder = Db::name('qt_product_order')
                                ->where(['room_id' => $v['id'],'pay_status'=>'0'])
                                ->order('id desc')
                                ->find();
                            $goodsPrice = Db::name('qt_product_order_item')
                                ->where(['status'=>'1','pay_status'=>'0'])
                                ->where('order_id',$goodsOrder['id'])->sum('goods_total_price');
                            $price = $projectPrice + $goodsPrice;
                            //未结账项目数量
                            $projectPay = Db::name('qt_order_project')
                                ->alias('op')
                                ->leftJoin('qt_order o', 'o.id = op.order_id')
                                ->leftJoin('qt_users u', 'op.engineer_id = u.id')
                                ->where(['o.room_id' => $v['id'],'op.pay_status'=>'0','op.is_change'=>'1','op.is_delete'=>'2'])
                                ->whereIn('op.status', ['2', '1','3'])
                                ->count();
                            $handCardName = Db::name('qt_order_project')
                                ->alias('op')
                                ->leftJoin('qt_hand_card h','h.id = op.hand_card_id')
                                ->where(['op.pay_status'=>'0','op.room_id' => $v['id'],'op.is_change'=>'1','op.is_delete'=>'2'])
                                ->whereIn('op.status', ['2', '1','3'])
                                ->where('is_change', '1')
                                ->group('h.name')
                                ->column('h.name');
                            if(!empty($handCardName)){
                                if($handCardName[0] == null){
                                    $handCardName = [];
                                }
                            }
                            $goodsHandCardName = Db::name('qt_product_order_item')
                                ->alias('item')
                                ->leftJoin('qt_hand_card c','c.id = item.hand_card_id')
                                ->where(['item.status'=>'1','item.pay_status'=>'0'])
                                ->where('item.order_id',$goodsOrder['id'])
                                ->group('c.name')
                                ->column('c.name');
                            //未结算商品数量
                            $goodsPay = Db::name('qt_product_order_item')
                                ->where('order_id',$goodsOrder['id'])
                                ->where(['status'=>'1'])
                                ->count();
                            $count = $projectPay+ $goodsPay;
                            $data[$k]['member'] = count($engineer);
                            foreach ($engineer as $kk => $vv) {
                                if ($vv == null) {
                                    unset($engineer[$kk]);
                                }
                            }
                            //查找各个技师的起钟时间
                            $engineerIds = Db::name('qt_order_queue')
                                ->where('room_id',$v['id'])
                                ->whereNotNull('start_time')
                                ->whereNull('real_time')
                                ->order('order_project_id desc')
                                ->column('end_time');
                            //项目的时间
                            $projectTime = Db::name('qt_order_project')
                                ->alias('op')
                                ->leftJoin('qt_project p', 'p.id = op.project_id')
                                ->where(['op.room_id' => $v['id'],'op.is_change'=>'1','op.is_delete'=>'2'])
                                ->whereIn('op.status', ['2', '3', '1'])
                                ->order('op.id desc')
                                ->column('p.time');
                            $orderCreateTime = Db::name('qt_order_project')
                                ->where(['room_id' => $v['id'],'is_change'=>'1','is_delete'=>'2','pay_status'=>'0'])
                                ->whereIn('status', ['2', '3', '1'])
                                ->order('id desc')
                                ->column('created_time');
                            $wait =  Db::name('qt_wait')->where(['room_id'=>$v['id'],'status'=>'1','delay_time'=>'0'])->count();
                            $data[$k]['wait'] = $wait;
                            $data[$k]['time'] = implode(',', $projectTime);
                            $data[$k]['engineer'] = implode(',', $engineer);
                            $data[$k]['order_create_time'] =  $orderCreateTime;
                            $data[$k]['price'] = $price;
                            $data[$k]['count'] = $count;
                            $data[$k]['end_time'] = $engineerIds;
                            $handCardData = array_merge($handCardName,$goodsHandCardName);
                            $data[$k]['hand_card_name'] = array_unique($handCardData);
                            //是否有预约
                            $appointment = Db::name('qt_room_appointment')->where(['room_id'=>$v['id'],'status'=>'1'])->find();
                            if(empty($appointment)){
                                $data[$k]['appointment'] = 0;//没有预约
                            }else{
                                $data[$k]['appointment'] = 1;//有预约
                            }
                            if(!empty($v['marge'])){
                                $roomIds = explode(',',$v['marge']);
                                $roomName = Db::name('qt_room')->whereIn('id',$roomIds)->column('name');
                                $data[$k]['marge_name'] = implode('-',$roomName);
                            }
                        } else {
                            $data[$k]['member'] = 0;
                            $data[$k]['engineer'] = '';
                            $data[$k]['price'] = 0;
                            $data[$k]['count'] = 0;
                            $data[$k]['time'] = null;
                            $data[$k]['end_time'] = null;
                            $data[$k]['marge_name']= '';
                            $data[$k]['appointment']= 0;
                            $data[$k]['wait']= 0;
                        }
                        $data[$k]['room_book'] = Db::name('qt_room_book')->where(['room_id'=>$v['id'],'status'=>'1'])->count();
                    }
                    $groupData[$key]['data'] = $data;
                }
            }
            $result['original'] = $originalData;
            $result['group'] = $groupData;
            $result['total_number'] = $total_member;
            $result['total_price'] = $total_price;
            $this->result('查询成功', $result, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 根据床位获取房间列表
     */
    public function engineer_bed()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['limit']) || empty($getData['limit'])) {
                $getData['limit'] = '10';
            }
            $where = [];
            //房间类型
            if (!isset($getData['bed_name']) || empty($getData['bed_name'])) {
                $data = Db::name('qt_room')
                    ->alias('r')
                    ->leftJoin('qt_room_type rt', 'rt.id = r.type_id')
                    ->leftJoin('qt_room_status rs', 'rs.id = r.status_id')
                    ->field('r.id,r.name,rt.name as type_name,rs.name as status_name,r.type_id')
                    ->where(['r.store_id' => $this->userInfo['store_id']])
                    ->where($where)
                    ->paginate($getData['limit'])->each(function ($item) {
                        if ($item['type_id'] == '2' || $item['type_id'] == '3' || $item['type_id'] == '4') {
                            $engineer = Db::name('qt_order_project')
                                ->field('op.engineer_id')
                                ->alias('op')
                                ->leftJoin('qt_order o', 'o.id = op.order_id')
                                ->where(['o.room_id' => $item['id']])
                                ->whereIn('o.status', ['2', '3', '4'])
                                ->select();
                            $item['member'] = count($engineer);
                            $item['engineer'] = $engineer;
                        } else {
                            $item['member'] = 0;
                            $item['engineer'] = [];
                        }
                        return $item;
                    });
            } else {
                $data = Db::name('qt_room_bed')
                    ->alias('rb')
                    ->leftJoin('qt_room r', 'rb.room_id = r.id')
                    ->leftJoin('qt_room_type rt', 'rt.id = r.type_id')
                    ->leftJoin('qt_room_status rs', 'rs.id = r.status_id')
                    ->field('r.id,r.name,rt.name as type_name,rs.name as status_name,r.type_id')
                    ->where(['r.store_id' => $this->userInfo['store_id'], 'rb.name' => $getData['bed_name']])
                    ->group('rb.room_id')
                    ->paginate($getData['limit'])->each(function ($item) {
                        if ($item['type_id'] == '2' || $item['type_id'] == '3' || $item['type_id'] == '4') {
                            $engineer = Db::name('qt_order_project')
                                ->field('op.engineer_id')
                                ->alias('op')
                                ->leftJoin('qt_order o', 'o.id = op.order_id')
                                ->where(['o.room_id' => $item['id']])
                                ->whereIn('o.status', ['2', '3', '4'])
                                ->select();
                            $item['member'] = count($engineer);
                            $item['engineer'] = $engineer;
                        } else {
                            $item['member'] = 0;
                            $item['engineer'] = [];
                        }
                        return $item;
                    });
            }
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 房间详情
     */
    public function detail()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['room_id']) || empty($getData['room_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $project = [];
            $product = [];
            $roomRes = Db::name('qt_room')->where(['id' => $getData['room_id']])->find();
            if (in_array($roomRes['status_id'], ['2', '3', '4'])) {
                $order = Db::name('qt_order')->where(['room_id' => $getData['room_id']])->find();
                //dump($order);die;
                $project = Db::name('qt_order_project')
                    ->field('op.id,op.name,op.num,op.price,op.created_time,u.real_name,op.start_time,op.end_time,op.engineer_id')
                    ->alias('op')
                    ->leftJoin('qt_users u', 'u.id = op.created_order_id')
                    ->where('op.order_id', $order['id'])
                    ->select();
                if (!empty($project)) {
                    foreach ($project as $key => $value) {
                        $project[$key]['type'] = '1';
                        $project[$key]['work_number'] = Db::name('qt_users')->where(['id'=>$value['engineer_id']])->value('work_number');
                    }
                }
                $order = Db::name('qt_product_order')->where(['room_id' => $getData['room_id']])->find();
                $product = Db::name('qt_product_order_item')
                    ->field('id,goods_name as name,payment_price as price,goods_num as num,create_time as created_time')
                    ->where(['order_id' => $order['id'],'status'=>'1'])
                    ->select();
                if (!empty($product)) {
                    foreach ($product as $key => $value) {
                        $product[$key]['real_name'] = Db::name('qt_user_viper')->where(['id' => $order['uv_id']])->value('nickname');
                        $product[$key]['type'] = '2';
                    }
                }
            }
            $data = array_merge($project, $product);
            $this->result('请求成功', $data, 200);
        }
        $this->result('无效的请求方式', '', 204);
    }

    /**
     *取消房间
     */
    public function close()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            if (empty($getData['room_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $data = Db::name('qt_room')
                ->field('id,name,status_id')
                ->where(['id' => $getData['room_id'], 'store_id' => $userInfo['store_id'], 'status_id' => '2'])
                ->find();
            if (empty($data) || $data['status_id'] != '2') {
                $this->result('房间状态有问题', '', 204);
            }
            try {
                Db::startTrans();
                //修改房间状态
                Db::name('qt_room')->where(['id' => $getData['room_id']])->update(['status_id' => '1']);
                $roomLog['room_id'] = $getData['room_id'];
                $roomLog['user_id'] = $userInfo['id'];
                $roomLog['status'] = '1';
                $roomLog['created_time'] = time();
                Db::name('qt_room_log')->insert($roomLog);
                Db::commit();
            } catch (Exception $exception) {
                Db::rollback();
                $this->result('取消失败', '', 204);
            }
            $this->result('取消成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 房费列表
     */
    public function price_list()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            if (empty($getData['page']) || empty($getData['limit'])) {
                $this->result('缺少参数', '', 204);
            }
            $data = Db::name('qt_room_price')
                ->field('rp.*,rt.name as type_name')
                ->alias('rp')
                ->leftJoin('qt_room_type rt', 'rt.id = rp.room_type')
                ->where('rp.store_id', $userInfo['store_id'])
                ->order('rp.id desc')
                ->paginate($getData['limit']);
            $this->result('查询成功', $data, 200);
        } else {
            $this->result('无效的请求', '', 204);
        }
    }


    /**
     * 添加房费
     */
    public function add_price()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $validate = new \app\web\validate\RoomPrice();
            $find = Db::name('qt_room_price')
                ->where(['room_type'=>$getData['room_type'],'store_id'=>$this->userInfo['store_id']])
                ->find();
            if(!empty($find)){
                $this->result('房间类型已经添加过了', '', 204);
            }
            $product_price = Db::name('qt_goods')
                ->where(['id'=>$getData['product_id']])
                ->where(['store_id'=>$this->userInfo['store_id']])
                ->field('id,name,price')
                ->value('price');
            $getData['price'] = $product_price;
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $getData['created_time'] = time();
            $getData['store_id'] = $this->userInfo['store_id'];
            $insert = Db::name('qt_room_price')->insert($getData);
            if ($insert) {
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 编辑获取房费
     */
    public function edit_price()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['id'])) {
                $this->result('缺少参数', '', 204);
            }
            $del = Db::name('qt_room_price')->where('id', $getData['id'])->find();
            if ($del) {
                $this->result('查询成功', $del, 200);
            }
            $this->result('查询失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 保存房费
     */
    public function save_price()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $validate = new \app\web\validate\RoomPrice();
            $product_price = Db::name('qt_goods')
                ->where(['id'=>$post['product_id']])
                ->where(['store_id'=>$this->userInfo['store_id']])
                ->field('id,name,price')
                ->value('price');
            $post['price'] = $product_price;
            if (!$validate->scene('add')->check($post)) {
                $this->result($validate->getError(), '', 204);
            }
            $find = Db::name('qt_room_price')
                ->where(['room_type'=>$post['room_type'],'store_id'=>$this->userInfo['store_id']])
                ->find();
            if($find['id'] != $post['id']){
                $this->result('房间类型已经添加过了', '', 204);
            }
            if (Db::name('qt_room_price')->where('id', $post['id'])->update($post)) {
                $this->result('保存成功', '', 200);
            }
            $this->result('保存失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除房费
     */
    public function delete_price()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['id'])) {
                $this->result('缺少参数', '', 204);
            }
            $room_price = Db::name('qt_room_price')->where('id', $getData['id'])->find();
            //结账
            $product_item = Db::name('qt_product_order_item')->where(['status'=>'1'])->where('goods_id',$room_price['product_id'])->find();
            //下单
            $check_item = Db::name('qt_check_out_item')->where([
                'link_type'=>2,
                'link_id'=>$room_price['product_id']
            ])->find();
            if (!empty($product_item) || !empty($check_item)) {
                $this->result('房费已经使用不可删除！', '', 204);
            }
            $del = Db::name('qt_room_price')->where('id', $getData['id'])->delete();
            if ($del) {
                $this->result('删除成功', '', 200);
            }
            $this->result('删除失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 房间二维码
     */
    public function qr_code()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['room_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $roomData = Db::name('qt_room')->where(['id'=>$getData['room_id']])->find();
//            if(empty($roomData['qr_code'])){
                $imageName = strtotime(date('Y-m-d')) . '-' . $this->userInfo['store_id'] .'-'.$getData['room_id']. '.png';
                $image = create_ewm(config('room_site_url').'/room?store_id'.$this->userInfo['store_id'].'&room_id=' . $getData['room_id'], 'uploads/qrcode/', $imageName, 10);
                Db::name('qt_room')->where(['id'=>$getData['room_id']])->update(['qr_code'=>$image]);
//            }else{
//                $image = $roomData['qr_code'];
//            }
            $result = config('web_site_url') . '/uploads/qrcode/' . $image;
            $this->result('查询成功', $result, 200);

        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 门店二维码
     */
    public function store_code()
    {
        if ($this->request->isPost()) {
            $imageName = strtotime(date('Y-m-d')) . '-' . $this->userInfo['store_id'] . '.png';
            $storeName = Db::name('qt_store')->where('id',$this->userInfo['store_id'])->value('name');
            $image = create_ewm(config('app.site_url').'/h5/#/?store_id=' . $this->userInfo['store_id'].'&store_name='.$storeName, 'uploads/qrcode/', $imageName, 10);
            $result = config('app.site_url') . '/uploads/qrcode/' . $image;
            $this->result('查询成功', $result, 200);

        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 保存图片
     */
    public function save_image()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['images']) || empty($getData['project_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $update = Db::name('qt_project')->where('id', $getData['project_id'])->update(['images' => $getData['images']]);
            if ($update !== false) {
                $this->result('更新成功', $update, 200);
            }
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 会做当前项目的技师
     */
    public function get_use_project()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['project_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $storeId = Db::name('qt_project')->where(['id'=>$getData['project_id']])->value('store_id');
            $data = Db::name('qt_user_project')
                ->field('p.id as project_id,u.real_name,u.work_number')
                ->alias('p')
                ->leftJoin('qt_users u', 'u.id = p.engineer_id')
                ->where('p.project_id', $getData['project_id'])
                ->where(['u.work_status'=>'1','u.status'=>'0','u.store_id'=>$storeId])
                ->select();
            $this->result('查询成功', $data, 200);

        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 会做当前在线会做项目的技师
     */
    public function get_user_line()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['project_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $storeId = Db::name('qt_project')->where(['id'=>$getData['project_id']])->value('store_id');
            //查询男女技师数量
            $man = Db::name('qt_user_project')
                ->field('p.id,u.real_name')
                ->alias('p')
                ->leftJoin('qt_users u', 'u.id = p.engineer_id')
                ->where('p.project_id', $getData['project_id'])
                ->where(['u.work_status'=>'1','u.sex'=>'1','u.status'=>'0','u.store_id'=>$storeId])
                ->count();
            $girl = Db::name('qt_user_project')
                ->field('p.id as project_id,u.real_name')
                ->alias('p')
                ->leftJoin('qt_users u', 'u.id = p.engineer_id')
                ->where('p.project_id', $getData['project_id'])
                ->where(['u.work_status'=>'1','u.sex'=>'2','u.status'=>'0','u.store_id'=>$storeId])
                ->count();
            $data['man'] = $man;
            $data['woman'] = $girl;
            $this->result('查询成功', $data, 200);

        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 会做当前项目的技师
     */
    public function save_use_project()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['project_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $del = Db::name('qt_user_project')->where('project_id', $getData['project_id'])->delete();
            $engineerIds = explode(',', $getData['engineer_ids']);
            foreach ($engineerIds as $key => $value) {
                $insert[$key]['engineer_id'] = $value;
                $insert[$key]['project_id'] = $getData['project_id'];
                $insert[$key]['create_time'] = time();
            }
            $save = Db::name('qt_user_project')->insertAll($insert);
            if ($del && $save) {
                $this->result('保存成功', '', 200);
            }
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 点call加钟会做项目的空闲技师
     */
    public function call_user_line()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['project_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $storeId = Db::name('qt_project')->where(['id'=>$getData['project_id']])->value('store_id');
            $sql = Db::name('qt_user_project')
                ->field('u.id as engineer_id,u.work_number,u.real_name')
                ->alias('p')
                ->leftJoin('qt_users u', 'u.id = p.engineer_id')
                ->leftJoin('qt_order_queue q', 'u.id = q.user_id')
                ->where('p.project_id', $getData['project_id'])
                ->where(['u.work_status'=>'1','u.status'=>'0','u.store_id'=>$storeId]);
            if (isset($getData['work_number']) && !empty($getData['work_number'])) {
                $sql->where('u.work_number', $getData['work_number']);
            }
            if(isset($getData['is_add']) && $getData['is_add'] == '2'){
                //查找正在加班的
                $sql->where('u.is_add', '2');
            }
            $data = $sql->order('q.sort asc')->select();
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 获取当前房间等待项目
     */
    public function wait()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['room_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $data = Db::name('qt_wait')
                ->alias('w')
                ->leftJoin('qt_project p', 'p.id = w.project_id')
                ->field('w.id,w.project_id,p.name as project_name')
                ->where(['w.room_id' => $getData['room_id'], 'w.status' => '1'])
                ->group('w.project_id')
                ->select();
            foreach ($data as $key => $value) {
                $data[$key]['count'] = Db::name('qt_wait')
                    ->where(['room_id' => $getData['room_id'], 'status' => '1', 'project_id' => $value['project_id']])
                    ->count();
            }
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 获取房间备注
     */
    public function get_remarks(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['room_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $marge = Db::name('qt_room')->where(['id'=>$getData['room_id']])->value('marge');
            if(empty($marge)){
                $data = Db::name('qt_room')->field('id,name,content')->where('id',$getData['room_id'])->select();
            }else{
                $roomIds = explode(',',$marge);
                $data = Db::name('qt_room')->field('id,name,content')->whereIn('id',$roomIds)->select();
            }
            $this->result('查询成功', $data, 200);
        }
    }


    /**
     * 获取房间净房规则
     */
    public function clean_rule(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['room_id'])) {
                $this->result('请选择房间号', '', 204);
            }
            $data = Db::name('qt_room')->field('clean_rule,clean_price,bed_num')->where(['id'=>$getData['room_id']])->find();
            $this->result('查询成功', $data, 200);
        }
    }



    /**
     * 批量下载房间二维码
     */
    public function roomZip(){
        $storeId = $this->request->get('store_id');
        $roomData = Db::name('qt_room')->field('id,name,qr_code')->where(['store_id'=>$storeId])->select();
        foreach ($roomData as $key=>$value){
            if(empty($value['qr_code'])){
                $imageName = strtotime(date('Y-m-d')) . '-' . $this->userInfo['store_id'] .'-'.$value['id']. '.png';
                $image = create_ewm(config('app.site_url').'?id=' . $value['id'], 'uploads/qrcode/', $imageName, 10);
                Db::name('qt_room')->where(['id'=>$value['id']])->update(['qr_code'=>$image]);
                $roomData[$key]['image_src'] = $image;
            }else{
                $roomData[$key]['image_src'] = $value['qr_code'];
            }
            $roomData[$key]['image_name'] = $value['name'].'.png';
            unset($roomData[$key]['id']);
            unset($roomData[$key]['name']);
            unset($roomData[$key]['qr_code']);
        }
        //将要下载的图片数组
//        $image[] = array(
//            'image_src' => "http://houtai1.kamiyunhui.com/uploads/qrcode/1664380800-1.png",
//            'image_name' => "1664380800-1.png"
//        );
        //下面是实例操作过程：
        $dfile = tempnam('/tmp', 'tmp');//产生一个临时文件，用于缓存下载文件
        $zip = new ZipFileService();
        //----------------------
        $filename = 'image.zip'; //下载的默认文件名

        //以下是需要下载的图片数组信息，将需要下载的图片信息转化为类似即可
        foreach ($roomData as $v) {
            $zip->add_file(file_get_contents($v['image_src']),
                iconv('utf-8', 'GB18030',$v['image_name']));
            // 添加打包的图片，第一个参数是图片内容，第二个参数是压缩包里面的显示的名称,
            //   可包含路径
            // 或是想打包整个目录 用 $zip->add_path($image_path);
        }
        //----------------------
        $zip->output($dfile);

        // 下载文件
        ob_clean();
        header('Pragma: public');
        header('Last-Modified:' . gmdate('D, d M Y H:i:s') . 'GMT');
        header('Cache-Control:no-store, no-cache, must-revalidate');
        header('Cache-Control:pre-check=0, post-check=0, max-age=0');
        header('Content-Transfer-Encoding:binary');
        header('Content-Encoding:none');
        header('Content-type:multipart/form-data');
        //设置下载的默认文件名
        header('Content-Disposition:attachment; filename="' . $filename . '"');
        header('Content-length:' . filesize($dfile));
        $fp = fopen($dfile, 'r');
        while (connection_status() == 0 && $buf = @fread($fp, 8192)) {
            echo $buf;
        }
        fclose($fp);
        @unlink($dfile);
        @flush();
        @ob_flush();
        exit;
    }






}
